﻿using Microsoft.EntityFrameworkCore;
using PowerHub.MES.Common;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PowerHub.MES.Repository.Basic
{
    /// <summary>
    /// 计量单位数据实现层
    /// </summary>
    public class BasicData_MeasuringUnitRepository : BaseRepository<BasicData_MeasuringUnit>, IBasicData_MeasuringUnitRepository
    {
        private PowerHubMesContext _repositorySys;
        public BasicData_MeasuringUnitRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }


        /// <summary>
        /// 添加计量单位
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> CreateBasicData_MeasuringUnitAsync(BasicData_MeasuringUnit query)
        {
            var result = new OperationResult<object>();
            if (AddEntity(query)>0)
            {
                result.Data = true;
            }
            else
            {
                result.Data = false;

            }

            return  result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> BatchDeletionBasicData_MeasuringUnitAsync(List<string>? ids)
        {
            var result = new OperationResult<object>();

            using (var transaction = _repositorySys.Database.BeginTransaction())
            {
                try
                {
                    var MeasuringUnits = GetEntitys().AsNoTracking().Where(p => ids.Contains(p.Id)).ToList();
                    foreach (var item in MeasuringUnits)
                    {
                        item.IsDel = 1;
                        GetEntitys().Update(item);
                    }

                    _repositorySys.SaveChanges();
                    transaction.Commit();
                    result.Message = "删除成功";
                    result.Data =true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }

            return result;
        }

        /// <summary>
        /// 获取计量单位列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<BasicData_MeasuringUnit>>> GetBasicData_MeasuringUnitPageListAsync(QueryBasicData_MeasuringUnitDTO query)
        {
            var result = new OperationPageResult<List<BasicData_MeasuringUnit>>();

            var currentQuery = GetEntitys().AsNoTracking()
                                            .Where(p => p.IsDel == 0).Where(p => p.MeasuringUnitName
                                            .Contains(query.MeasuringUnitName ?? string.Empty));

            result.PageIndex = query.PageIndex;
            result.PageSize = query.PageSize;
            result.TotalCount = await currentQuery.CountAsync();

            if (result.TotalCount > 0)
            {
                result.Data = await currentQuery.OrderByDescending(g => g.Createtime).Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).ToListAsync();
            }

            return result;
        }

        /// <summary>
        /// 更新计量单位
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> UpdateBasicData_MeasuringUnitAsync(BasicData_MeasuringUnit query)
        {
            var result = new OperationResult<object>();
            if (UpdataEntity(query) > 0)
            {
                result.Data = true;
            }
            else
            {
                result.Data = false;

            }

            return result;
        }
    }
}
